//
//  AnomalyShapeChangeView.m
//  测试
//
//  Created by admin on 2017/9/2.
//  Copyright © 2017年 DK. All rights reserved.
//

#import "AnomalyShapeChangeView.h"

#define k_POINT_WIDTH 4

@interface AnomalyShapeChangeView ()

@property (nonatomic, strong) NSArray *lastPoints;// 添加之前的数组 (上一次展示的数组)

@end

@implementation AnomalyShapeChangeView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
#pragma mark - 第一次添加点   (默认位置)
- (void)addInitialPoints:(NSArray *)points {
    if (3 > points.count) {
        return;
    }
    NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:0];
    for (NSValue *value in points) {
        UIView *point = [self addPointViewAt:value.CGPointValue];
        [self addSubview:point];
        [pointArray addObject:point];
    }
    self.points = [NSMutableArray arrayWithArray:pointArray];
    self.lastPoints = self.points;
}
#pragma mark - 在特定位置添加点
- (void)addPointAt:(CGPoint)point {
    // 找出距离添加点最近的点 (下标)
    NSInteger nearest = 0;
    CGFloat nearestDistance = 10000;
    for (int i = 0; i < self.lastPoints.count; i ++) {
        UIView *view = self.lastPoints[i];
        CGPoint toPoint = view.center;
        CGFloat distance = [self distanceFrom:point to:toPoint];
        if (distance < nearestDistance) {
            nearestDistance = distance;
            nearest = i;
        }
    }
    // 创建新添点视图
    UIView *newPoint = [self addPointViewAt:point];
    [self addSubview:newPoint];
    [self.points insertObject:newPoint atIndex:nearest + 1];
    [self setNeedsDisplay];
}
#pragma mark 两点之间的距离
- (CGFloat)distanceFrom:(CGPoint)origin to:(CGPoint)destination {
    CGFloat xDist = (origin.x - destination.x);
    if (0 > xDist) {
        xDist = xDist * -1;
    }
    CGFloat yDist = (origin.y - destination.y);
    if(0 > yDist) {
        yDist = yDist * -1;
    }
    return sqrt((xDist * xDist) + (yDist * yDist));
}
#pragma mark - 添加点视图
- (UIView *)addPointViewAt:(CGPoint)point {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(point.x - k_POINT_WIDTH / 2, point.y - k_POINT_WIDTH / 2, k_POINT_WIDTH, k_POINT_WIDTH)];
    view.backgroundColor =[UIColor greenColor];
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = view.frame.size.width / 2;
    return view;
}
#pragma mark - 获取页面上所有的点
- (NSMutableArray *)getAllPoints {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:0];
    for (UIView *point in self.points) {
        NSValue *value = [NSValue valueWithCGPoint:point.center];
        [mArray addObject:value];
    }
    return mArray;
}
#pragma mark - 画线
- (void)drawRect:(CGRect)rect {
    if (!self.points.count) {
        return;
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextClearRect(context, self.frame);
    
    const CGFloat *components = CGColorGetComponents([UIColor yellowColor].CGColor);
    CGFloat red;
    CGFloat green;
    CGFloat blue;
    CGFloat alpha;
    if(CGColorGetNumberOfComponents([UIColor yellowColor].CGColor) == 2) {
        red   = 1;
        green = 1;
        blue  = 1;
        alpha = 1;
    } else {
        red   = components[0];
        green = components[1];
        blue  = components[2];
        alpha = components[3];
        if (alpha <= 0) alpha = 1;
    }
    // set the stroke color and width
    CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
    CGContextSetLineWidth(context, 2.0);// 设置线的宽度
    
    UIView *point1 = [self.points objectAtIndex:0];
    // 画一条线
    CGContextMoveToPoint(context, point1.frame.origin.x + k_POINT_WIDTH / 2, point1.frame.origin.y + k_POINT_WIDTH / 2);
    for (int i = 1; i < self.points.count; i++) {
        UIView *point = [self.points objectAtIndex:i];
        CGContextAddLineToPoint(context, point.frame.origin.x + k_POINT_WIDTH / 2, point.frame.origin.y + k_POINT_WIDTH / 2);
    }
    CGContextAddLineToPoint(context, point1.frame.origin.x + k_POINT_WIDTH / 2, point1.frame.origin.y + k_POINT_WIDTH / 2);
    // tell the context to draw the stroked line
    // 开始画线
    CGContextStrokePath(context);
}



@end
